/**
 * 
 */
package org.xlab.baseframework.web.component;

import org.vaadin.jouni.animator.Animator;
import org.vaadin.jouni.animator.AnimatorProxy;
import org.vaadin.jouni.animator.client.ui.VAnimatorProxy.AnimType;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.web.images.ImageStreamSource;
import org.xlab.baseframework.web.util.BrowserUtil;

import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.terminal.gwt.server.WebBrowser;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * @author MaKuBEX
 * 
 */
public class SystemMainWindow extends Window {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 6054338078703184129L;

	private TopMenuLayout topMenu;
	private Layout mainLayout;
	private AnimatorProxy ani_mainLayout;

	public SystemMainWindow() {
		super();
		super.setBorder(Window.BORDER_NONE);
		super.setSizeFull();
	}

	/**
	 * @param caption
	 */
	public SystemMainWindow(String caption) {
		super(caption);
		super.setSizeFull();
	}

	/**
	 * @param topMenu
	 *            the topMenu to set
	 */
	public void setTopMenu(TopMenuLayout topMenu) {
		this.topMenu = topMenu;
	}

	public void showMain() {
		super.removeAllComponents();
		super.addComponent(topMenu);
		ani_mainLayout = new AnimatorProxy();
		super.addComponent(ani_mainLayout);
		ISystemUserDomain user = (ISystemUserDomain) super.getApplication()
				.getUser();
		this.showNotification(
				"Welcome",
				"<b>MaKuBEX:</b> Welcome " + user.getRealName()
						+ "<br/> Your email address is: " + user.getUserEmail(),
				Notification.TYPE_TRAY_NOTIFICATION);
	}

	public void showWelcome() {
		super.removeAllComponents();
		HorizontalLayout mainLayout;
		VerticalLayout rightLayout;
		LoginLayout loginLayout;
		WebBrowser webBrowser = ((WebApplicationContext) super.getApplication()
				.getContext()).getBrowser();
		mainLayout = new HorizontalLayout();
		mainLayout.setSizeFull();
		mainLayout.setSpacing(false);
		rightLayout = new VerticalLayout();
		
		VerticalLayout browserLayout = new VerticalLayout();
		Label ipAddresslabel = new Label("Hello user from <b>"
				+ webBrowser.getAddress() + "</b>.", Label.CONTENT_XHTML);
		Label browser = new Label("You are running <b>"
				+ BrowserUtil.getBrowserType(webBrowser)
				+ webBrowser.getBrowserMinorVersion() + " in "
				+ BrowserUtil.getOperatingSystem(webBrowser) + "</b>.",
				Label.CONTENT_XHTML);
		Label screenSize = new Label("Your screen resolution is <b>"
				+ webBrowser.getScreenWidth() + "x"
				+ webBrowser.getScreenHeight() + "</b>.", Label.CONTENT_XHTML);
		Label locale = new Label("Your browser is set to primarily use the <b>"
				+ webBrowser.getLocale() + "</b> locale.", Label.CONTENT_XHTML);
		browserLayout.addComponent(ipAddresslabel);
		browserLayout.addComponent(browser);
		browserLayout.addComponent(screenSize);
		browserLayout.addComponent(locale);
		browserLayout.setWidth("100%");

		loginLayout = new LoginLayout();

		rightLayout.addComponent(browserLayout);
		rightLayout.addComponent(loginLayout);

		StreamResource imageresource = new StreamResource(
				ImageStreamSource.ARCHITECTURE, "architecture.png",
				super.getApplication());
		Embedded architecture = new Embedded();
		architecture.setSource(imageresource);

		Animator ani_architecture = new Animator(architecture);
		ani_architecture.fadeIn(3000, 500);
		Animator ani_rightLayout = new Animator(rightLayout);
		ani_rightLayout.fadeIn(1000, 2000);

		mainLayout.addComponent(ani_architecture);
		mainLayout.addComponent(ani_rightLayout);

		super.addComponent(mainLayout);
	}
	
	public void clearMainLayout() {
		if (this.mainLayout != null) {
			ani_mainLayout.animate(this.mainLayout, AnimType.FADE_OUT)
					.setDuration(500);
			super.removeComponent(this.mainLayout);
		}
	}

	public void updateMainLayout(Layout newLayout) {
		this.clearMainLayout();
		this.mainLayout = newLayout;
		ani_mainLayout.animate(this.mainLayout, AnimType.FADE_IN)
				.setDuration(500).setDelay(100);
		super.addComponent(this.mainLayout);
	}
}
